/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package media_server;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.media.*;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/**
 *
 * @author ThanhLam
 */
public class Video extends JFrame implements ControllerListener
{
   Component vc;//,cc;
   Player player;
   String[] dd1 = null;//JList l = new JList();
   int j;
   String tl;
   String p,p1,p2,p3,p4;
   

   Video (String title, String[] dd, int index,String port)
   {
      super (title);
      tl = title;
      p = port;
      addWindowListener (new WindowAdapter ()
                         {
                             public void windowClosing (WindowEvent e)
                             {
                                player.stop();
                                //AVTransmit2.at.stop();
                               // AVTransmit2.at.stop();
                                dispose();
                                //System.exit (0);
                             }
                         } );

       dd1 = new String[dd.length];
       for(int i = 0; i< dd.length; i++)
       {
             dd1[i] = dd[i];
       }       
       j = index;
       String path = dd[index];
       String[] ddd = path.split("[/#$@^%]");
        String url = "file:";
        for(int i = 0; i< ddd.length; i++)
        {
            if(i == 0)
            {
                url = url + ddd[i];
            }
            else
            {
                 url = url + "/" + ddd[i];
            }
        }

      try
      {
         player = Manager.createPlayer (new MediaLocator (url));
      }
      catch (java.io.IOException e)
      {
         terminate (e.toString ());
      }
      catch (NoPlayerException e)
      {
         terminate ("Could not find a player.");
      }

      if (player == null)
          terminate ("Trouble creating player.");

      player.addControllerListener (this);
      player.start();
      AVTransmit2.StartTransmint(url,ServerMain.ip,port);
   }

   public void controllerUpdate (ControllerEvent e)
   {
      if (e instanceof ControllerClosedEvent)
          System.exit (0);

      if (e instanceof EndOfMediaEvent)
      {
          if(j == dd1.length -1)
          {
           // AVTransmit2.at.stop();
             dispose();
           // new Video(tl,dd1,0,p);
          }
          else
          {
           // AVTransmit2.at.stop();
             dispose();
           // new Video(tl,dd1,j + 1,p);
          }
      }

      if (e instanceof PrefetchCompleteEvent)
      {
          player.start();
          return;
      }

     if (e instanceof RealizeCompleteEvent)
      {
          vc = player.getVisualComponent ();
          if (vc != null)
              add (vc, BorderLayout.CENTER);
//          cc = player.getControlPanelComponent();
//          add(cc, BorderLayout.SOUTH);
          
          pack ();
          setResizable (false);
          setVisible (true);
      }


   }

   void terminate (String s)
   {
      System.out.println (s);
      System.exit (-1);
   }   

}

